package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domian.Dish;
import com.itheima.domian.Flavor;
import com.itheima.domian.Setmeal;
import com.itheima.domian.SetmealDish;
import com.itheima.domian.vo.DishVo;
import com.itheima.domian.vo.QueryVo2;
import com.itheima.domian.vo.SetmealVo;
import com.itheima.mapper.admin.FlavorMapper;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.service.admin.DishService;
import com.itheima.service.admin.FlavorService;
import com.itheima.service.admin.SetmealDishService;
import com.itheima.service.admin.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 12441
 * @Date 2022/8/11 15:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    @Autowired
    private FlavorMapper flavorMapper;



    @GetMapping("/list")
    public R<List<Setmeal>> SetmealList(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        log.info("===========================");
        //排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);

        return  R.success(list);
    }

@GetMapping("/dish/{id}")
public R<List<DishVo>> viewDish(@PathVariable("id") String id){


    LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(SetmealDish::getSetmealId,id);

    List<SetmealDish> list = setmealDishService.list(queryWrapper);

    List<DishVo> dishVoList = list.stream().map((item) -> {
        Long dishId1 = item.getDishId();
        Dish byId = dishService.getById(dishId1);
        LambdaQueryWrapper<Flavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Flavor::getDishId,byId.getId());
        List<Flavor> flavors = flavorMapper.selectList(wrapper);
        DishVo dishVo = new DishVo();
        BeanUtils.copyProperties(byId,dishVo);
        dishVo.setFlavors(flavors);
        return dishVo;
    }).collect(Collectors.toList());

    return  R.success(dishVoList,"成功");
}


}
